package com.ruoyi.product.service.impl;

import java.util.Date;
import java.util.List;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.BizCodeConstants;
import com.ruoyi.common.constant.CommonConst;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.IdGenerator;
import com.ruoyi.product.domain.Product;
import com.ruoyi.product.domain.ProductImg;
import com.ruoyi.product.domain.ProductPrice;
import com.ruoyi.product.dto.FavoritesAddParam;
import com.ruoyi.product.dto.PageQuery;
import com.ruoyi.product.dto.ProductBrowseDTO;
import com.ruoyi.product.dto.ProductFavoritesDTO;
import com.ruoyi.product.mapper.ProductMapper;
import com.ruoyi.product.service.IProductImgService;
import com.ruoyi.product.service.IProductPriceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.product.mapper.ProductFavoritesMapper;
import com.ruoyi.product.domain.ProductFavorites;
import com.ruoyi.product.service.IProductFavoritesService;
import com.ruoyi.common.core.text.Convert;

/**
 * 商品收藏Service业务层处理
 *
 * @author tanfp
 * @date 2020-12-28
 */
@Service
public class ProductFavoritesServiceImpl implements IProductFavoritesService {


    @Autowired
    private ProductFavoritesMapper productFavoritesMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private IProductImgService productImgService;

    @Autowired
    private IProductPriceService productPriceService;

    @Override
    public AjaxResult addFavorites(Long userId, Long productId) {
        // 查询商品是否存在
        Product queryProduct = new Product();
        queryProduct.setKeyId(productId);
        queryProduct.setDelStatus(CommonConst.DEL_FALSE);
        List<Product> products = productMapper.selectProductList(queryProduct);
        if (CollUtil.isEmpty(products)) {
            return AjaxResult.error(BizCodeConstants.PRODUCT_NOT_EXIST, "商品不存在");
        }

        ProductFavorites query = new ProductFavorites();
        query.setDelStatus(CommonConst.DEL_FALSE);
        query.setUserId(userId);
        query.setProductId(productId);
        List<ProductFavorites> productFavorites = productFavoritesMapper.selectProductFavoritesList(query);
        if (CollUtil.isEmpty(productFavorites)) {
            // 新增收藏
            ProductFavorites insert = new ProductFavorites();
            insert.setKeyId(IdGenerator.getId());
            insert.setUserId(userId);
            insert.setCreateBy(String.valueOf(userId));
            insert.setCreateTime(new Date());
            insert.setUpdateBy(insert.getCreateBy());
            insert.setUpdateTime(insert.getCreateTime());
            insert.setDelStatus(0);
            Product p = products.get(0);
            ProductImg img = productImgService.selectProductImgById(p.getImgId());
            insert.setProductImg(img.getImgUrl());
            ProductPrice price = productPriceService.selectProductPriceById(p.getPriceId());
            insert.setProductOrignPrice(price.getProductOriginPrice());
            insert.setProductPrice(price.getProductPrice());
            insert.setUnit(price.getUnit());
            insert.setProductId(productId);
            productFavoritesMapper.insertProductFavorites(insert);
        } else {
            return AjaxResult.error(BizCodeConstants.PRODUCT_FAVORITED, "商品已收藏");
        }
        return AjaxResult.success();
    }
    @Override
    public AjaxResult addFavorites(Long userId, FavoritesAddParam param) {
        Long productId = param.getProductId();
        // 查询商品是否存在
        Product queryProduct = new Product();
        queryProduct.setKeyId(productId);
        queryProduct.setDelStatus(CommonConst.DEL_FALSE);
        List<Product> products = productMapper.selectProductList(queryProduct);
        if (CollUtil.isEmpty(products)) {
            return AjaxResult.error(BizCodeConstants.PRODUCT_NOT_EXIST, "商品不存在");
        }

        ProductFavorites query = new ProductFavorites();
        query.setDelStatus(CommonConst.DEL_FALSE);
        query.setUserId(userId);
        query.setProductId(productId);
        List<ProductFavorites> productFavorites = productFavoritesMapper.selectProductFavoritesList(query);
        if (CollUtil.isEmpty(productFavorites)) {
            // 新增收藏
            ProductFavorites insert = new ProductFavorites();
            BeanUtils.copyProperties(param, insert);
            insert.setKeyId(IdGenerator.getId());
            insert.setUserId(userId);
            insert.setCreateBy(String.valueOf(userId));
            insert.setCreateTime(new Date());
            insert.setUpdateBy(insert.getCreateBy());
            insert.setUpdateTime(insert.getCreateTime());
            productFavoritesMapper.insertProductFavorites(insert);
        } else {
            return AjaxResult.error(BizCodeConstants.PRODUCT_FAVORITED, "商品已收藏");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult delFavorites(Long userId, List<String> productIds) {
        if (CollUtil.isEmpty(productIds)) {
            return AjaxResult.success();
        }
        productFavoritesMapper.delProductFavorites(userId, productIds);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult queryFavorites(Long userId, PageQuery pageQuery) {
        // 通过用户id获取该用户所有的收藏商品
        ProductFavorites query = new ProductFavorites();
        query.setUserId(userId);
        query.setDelStatus(CommonConst.DEL_FALSE);
        List<ProductFavorites> productFavorites = productFavoritesMapper.selectProductFavoritesList(query);
        if (CollUtil.isEmpty(productFavorites)) {
            return AjaxResult.success();
        }
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        List<ProductFavoritesDTO> productFavoritesDTOS = productFavoritesMapper.selectListByUserId(userId);
        PageInfo<ProductFavoritesDTO> pageInfo = PageInfo.of(productFavoritesDTOS);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 查询商品收藏
     *
     * @param keyId 商品收藏ID
     * @return 商品收藏
     */
    @Override
    public ProductFavorites selectProductFavoritesById(Long keyId) {
        return productFavoritesMapper.selectProductFavoritesById(keyId);
    }

    /**
     * 查询商品收藏列表
     *
     * @param productFavorites 商品收藏
     * @return 商品收藏
     */
    @Override
    public List<ProductFavorites> selectProductFavoritesList(ProductFavorites productFavorites) {
        return productFavoritesMapper.selectProductFavoritesList(productFavorites);
    }

    /**
     * 新增商品收藏
     *
     * @param productFavorites 商品收藏
     * @return 结果
     */
    @Override
    public int insertProductFavorites(ProductFavorites productFavorites) {
        productFavorites.setCreateTime(DateUtils.getNowDate());
        return productFavoritesMapper.insertProductFavorites(productFavorites);
    }

    /**
     * 修改商品收藏
     *
     * @param productFavorites 商品收藏
     * @return 结果
     */
    @Override
    public int updateProductFavorites(ProductFavorites productFavorites) {
        productFavorites.setUpdateTime(DateUtils.getNowDate());
        return productFavoritesMapper.updateProductFavorites(productFavorites);
    }

    /**
     * 删除商品收藏对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteProductFavoritesByIds(String ids) {
        return productFavoritesMapper.deleteProductFavoritesByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商品收藏信息
     *
     * @param keyId 商品收藏ID
     * @return 结果
     */
    @Override
    public int deleteProductFavoritesById(Long keyId) {
        return productFavoritesMapper.deleteProductFavoritesById(keyId);
    }

}
